package com.ruif.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.ruif.model.Tempera;
import com.ruif.util.DateUtil;
import com.ruif.util.StringUtil;

public class TemperaDAO
{
    
    /**
     * 查询（条件为空  返回 tempera 表所有记录）
     * @param conn
     * @param tem
     * @return
     * @throws Exception
     */
    public ResultSet list(Connection conn, Tempera tem) throws Exception
    {
        StringBuffer sb = new StringBuffer("select * from t_tempera");
        if (tem != null)
        {
            if (Integer.valueOf(tem.getTemp()) != Integer.valueOf(0))
            {
                sb.append(" and temp='" + tem.getTemp() + "'");
            }
            if (!StringUtil.isEmpty(tem.getDate()))
            {
                sb.append(" and date like '%" + tem.getDate() + "%'");
            }
            if (!StringUtil.isEmpty(tem.getStatus() + ""))
            {
                sb.append(" and status='" + tem.getStatus() + "'");
            }
        }
        PreparedStatement stmt = conn.prepareStatement(sb.toString().replaceFirst("and", "where"));
        return stmt.executeQuery();
    }
    
    /**
     * 新增 Tempera 记录
     * @param conn
     * @param tem
     * @return
     * @throws Exception
     */
    public int add(Connection conn, Tempera tem) throws Exception
    {
        String sql = "insert into t_tempera values(null, ?, ?, ?)";
        PreparedStatement stmt = conn.prepareStatement(sql);
        stmt.setInt(1, tem.getTemp());
        stmt.setString(2, DateUtil.getCurrentDate("yyyy/MM/dd HH:mm:ss"));
        stmt.setInt(3, tem.getStatus());
        return stmt.executeUpdate();
    }
    
    /**
     * 根据主键 ID 删除 Tempera 记录
     * @param conn
     * @param id
     * @return
     * @throws Exception
     */
    public int delete(Connection conn, String id) throws Exception
    {
        String sql = "delete from t_tempera where id=?";
        PreparedStatement stmt = conn.prepareStatement(sql);
        stmt.setString(1, id);
        return stmt.executeUpdate();
    }
    
    public int modify(Connection conn, Tempera tem) throws Exception
    {
        String sql = "update t_tempera set temp=?, date=?, status=? where id=?";
        PreparedStatement stmt = conn.prepareStatement(sql);
        stmt.setString(1, tem.getTemp() + "");
        stmt.setString(2, tem.getDate());
        stmt.setString(3, tem.getStatus() + "");
        stmt.setString(4, tem.getId() + "");
        return stmt.executeUpdate();
    }
    
}
